package com.kandinfo.survey.common.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kandinfo.base.common.dto.ResultDto;
import com.kandinfo.base.common.dto.ResultDtoFactory;
import com.kandinfo.base.common.service.AbstractService;
import com.kandinfo.base.security.SecurityContext;
import com.kandinfo.base.security.domain.User;
import com.kandinfo.base.security.service.UserService;
import com.kandinfo.survey.common.constants.StatusConstant;
import com.kandinfo.survey.common.domain.PatientFamilyRela;
import com.kandinfo.survey.common.dto.PatientFamilyDto;
import com.kandinfo.survey.common.repository.PatientFamilyRelaRepository;
import com.kandinfo.survey.common.service.PatientFamilyRelativeService;

@Service
public class PatientFamilyRelativeServiceImpl extends
        AbstractService<PatientFamilyRela, Long, PatientFamilyRelaRepository> implements PatientFamilyRelativeService {

    @Resource
    public void setPatientFamillyRelaRepository(PatientFamilyRelaRepository patientFamilyRelaRepository) {
        super.setRepository(patientFamilyRelaRepository);
    }

    // private static final Logger logger = LoggerFactory.getLogger(PatientFamillyRelativeServiceImpl.class);

    @Autowired
    UserService userService;

    @Override
    @Transactional
    public ResultDto connectFamily(PatientFamilyDto patientFamillyDto) {
        if (patientFamillyDto == null || StringUtils.isBlank(patientFamillyDto.getPatientMobile())
                || StringUtils.isBlank(patientFamillyDto.getPatientUserName())) {
            return ResultDtoFactory.toNack("手机号和姓名都必须填写");
        }
        User patientFamily = SecurityContext.getInstance().getCurrentUser();
        User patient = userService.findByUserNameAndMobile(patientFamillyDto.getPatientUserName(),
                patientFamillyDto.getPatientMobile());
        if (patient == null) { // case 2 : 默认的用户名（手机号）
            patient = userService.findByUserNameAndMobile(patientFamillyDto.getPatientMobile(),
                    patientFamillyDto.getPatientMobile());
        }
        if (patient == null) {
            return ResultDtoFactory.toNack("查询不到名为：" + patientFamillyDto.getPatientUserName() + "的用户");
        }

        PatientFamilyRela domain = new PatientFamilyRela();
        domain.setPatientId(patient.getId());
        domain.setFamilyId(patientFamily.getId());
        domain.setStatus(StatusConstant.NORMALLY_USE);

        repository.save(domain);
        return ResultDtoFactory.toAck("关联成功!");
    }

    @Override
    public ResultDto getConnectedFamily() {
        User patientFamily = SecurityContext.getInstance().getCurrentUser();
        
        PatientFamilyRela domain = repository.findByFamilyId(patientFamily.getId());
        if (domain == null) {
            return ResultDtoFactory.toAckNomessage(null);
        }
        User patient = userService.findOne(domain.getPatientId());
        if (patient == null) {
            return ResultDtoFactory.toAckNomessage(null);
        }
        PatientFamilyDto response = new PatientFamilyDto();
        response.setPatientMobile(patient.getMobile());
        response.setPatientUserName(StringUtils.isEmpty(patient.getName()) ? patient.getMobile() : patient.getName());
        
        return ResultDtoFactory.toAckNomessage(response);
    }

}
